package com.ruoyi.framework.web.service;

import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.GiveType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wq.domain.WqComment;
import com.ruoyi.wq.domain.WqCommentGive;
import com.ruoyi.wq.domain.WqDynamic;
import com.ruoyi.wq.domain.WqNoticeGive;
import com.ruoyi.wq.service.IWqCommentGiveService;
import com.ruoyi.wq.service.IWqCommentService;
import com.ruoyi.wq.service.IWqDynamicService;
import com.ruoyi.wq.service.IWqNoticeGiveService;
import com.ruoyi.wx.domain.WxMpMsgForm;
import com.ruoyi.wx.service.WxMsgService;
import cn.hutool.core.date.DateUtil;

@Component
public class CommentGiveService {
	
	@Autowired
    private ISysUserService sysUserService;
	
	@Autowired
	private IWqCommentService wqCommentService;
	
	@Autowired
	private IWqDynamicService wqDynamicService;
	
	@Autowired
	private IWqNoticeGiveService wqNoticeGiveService;
	
	@Autowired
	private IWqCommentGiveService wqCommentGiveService;
	
	@Autowired
    private WxMsgService wxMsgService;

	public Boolean insertWqCommentGive(WqCommentGive wqCommentGive){
    	// 是否点赞
    	Boolean isGive = false;
    	
    	SysUser sysUser = sysUserService.getUserInfo(SecurityUtils.getUserId());
    	WqComment comment = wqCommentService.getById(wqCommentGive.getCommentId());
    	if(sysUser.getUserId().equals(comment.getUserId())){
    		// 不能给自己点赞
    		return true;
    	}
    	WqDynamic dynamic = wqDynamicService.getById(comment.getDynamicId());
    	WqCommentGive give = getWqCommentGive(wqCommentGive.getCommentId(), sysUser.getUserId());
    	if(give == null){
    		isGive = true;
    		wqCommentGive.setUserId(sysUser.getUserId());
    		wqCommentGive.setAvatar(sysUser.getAvatar());
    		wqCommentGive.setNickName(sysUser.getNickName());
    		wqCommentGive.setPhone(sysUser.getPhonenumber());
    		wqCommentGive.setSex(sysUser.getSex());
    		wqCommentGiveService.save(wqCommentGive);
        	
        	// 点赞通知
        	WqNoticeGive noticeGive = new WqNoticeGive();
        	noticeGive.setDynamicId(dynamic.getId());
        	if(StringUtils.isNotBlank(dynamic.getImage())){
        		noticeGive.setDynamicImage(dynamic.getImage().split(",")[0]);
        	}
        	noticeGive.setCommentId(comment.getId());
        	noticeGive.setGiveType(GiveType.COMMENT_GIVE.getCode());
        	
        	noticeGive.setUserId(sysUser.getUserId());
        	noticeGive.setAvatar(sysUser.getAvatar());
        	noticeGive.setNickName(sysUser.getNickName());
        	noticeGive.setPhone(sysUser.getPhonenumber());
        	noticeGive.setSex(sysUser.getSex());
        	
        	noticeGive.setNoticeUserId(comment.getUserId());
        	wqNoticeGiveService.save(noticeGive);
        	
        	// 发送微信订阅消息
        	SysUser user = sysUserService.getById(comment.getUserId());
            if(StringUtils.isNotBlank(user.getAppId())){
            	if(StringUtils.isNotBlank(user.getWxOpenId())){
            		WxMpMsgForm msgForm = new WxMpMsgForm();
            		msgForm.setWxId(user.getWxId());
            		msgForm.setAppId(user.getAppId());
            		msgForm.setOpenId(user.getWxOpenId());
            		Map<String,String> dataMap = new HashMap<>();
            		dataMap.put("first", "您好，您收到一条动态点赞");
            		dataMap.put("keyword1", sysUser.getNickName());
            		dataMap.put("keyword2", DateUtil.now());
            		dataMap.put("keyword3", "您有一个新的点赞");
            		dataMap.put("remark", "本条信息是站内信通知，祝您早日脱单！点击查看详情");
            		msgForm.setDataMap(dataMap);
            		msgForm.setPage("pages/index?index=1");
            		wxMsgService.sendTemplateMsg(msgForm);
            	}
            }
            
    	}else{
    		isGive = false;
    		wqCommentGiveService.removeById(give.getId());
    		wqNoticeGiveService.deleteByCommentIdAndUserId(comment.getId(), sysUser.getUserId());
    	}
    	// 统计动态点赞数量
    	Integer giveNum = getGiveNumByCommentId(wqCommentGive.getCommentId());
    	
    	// 更新评论点赞数量
    	comment.setGiveNum(giveNum);
    	wqCommentService.updateById(comment);
    	
    	// 更新用户未读消息数量
    	SysUser user = sysUserService.getById(comment.getUserId());
    	Integer giveNewNum = wqNoticeGiveService.getUnReadNum(comment.getUserId());
    	user.setGiveNewNum(giveNewNum);
    	sysUserService.updateById(user);
    	return isGive;
    }
	
	private WqCommentGive getWqCommentGive(String commentId, Long userId){
    	QueryWrapper<WqCommentGive> qw = new QueryWrapper<>();
    	WqCommentGive entity = new WqCommentGive();
    	entity.setCommentId(commentId);
    	entity.setUserId(userId);
    	qw.setEntity(entity);
    	return wqCommentGiveService.getOne(qw);
    }
	
	private Integer getGiveNumByCommentId(String commentId){
    	QueryWrapper<WqCommentGive> qw = new QueryWrapper<>();
    	WqCommentGive entity = new WqCommentGive();
    	entity.setCommentId(commentId);
    	entity.setStatus("0");
    	qw.setEntity(entity);
    	return wqCommentGiveService.count(qw);
    }
	
}
